/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */

/**
 * Constructor
 */ 
function ValidatorUtils(){
	this.jsjava_class="jsorg.eob.validator.ValidatorUtils";
}

/**
 * Judge whether the value is null
 * @param value
 */
ValidatorUtils.isNull=function(value){
	if(value==undefined||value==null){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is null or empty
 * @param value
 */
ValidatorUtils.isBlankOrNull=function(value){
	if(ValidatorUtils.isNull(value)){
		return true;
	}
	if(value instanceof String||typeof(value)=="string"){
		if(value==""||/^\s*$/.test(value)){
			return true;
		}
		return false;
	}
	if(value instanceof Collection||value instanceof Map){
		if(value.size()==0){
			return true;
		}
		return false;
	}
	if(value instanceof Array){
		return value.length==0;
	}
	return false;
};

/**
 * Judge whether the value is a byte
 * @param value
 */
ValidatorUtils.isByte=function(value){
	return Byte.checkValid(value);
};

/**
 * Judge whether the value is character
 * @param value
 */
ValidatorUtils.isChar=function(value){
	return Character.checkValid(value);
};

/**
 * Judge whether the value is short
 * @param value
 */
ValidatorUtils.isShort=function(value){
	return Short.checkValid(value);
};

/**
 * Judge whether the value is integer
 * @param value
 */
ValidatorUtils.isInt=function(value){
	return Integer.checkValid(value);
};

/**
 * Judge whether the value is long
 * @param value
 */
ValidatorUtils.isLong=function(value){
	return Long.checkValid(value);
};

/**
 * Judge whether the value is float
 * @param value
 */
ValidatorUtils.isFloat=function(value){
	return Float.checkValid(value);
};

/**
 * Judge whether the value is double
 * @param value
 */
ValidatorUtils.isDouble=function(value){
	return Double.checkValid(value);
};

/**
 * Judge whether the value is boolean
 * @param value
 */
ValidatorUtils.isBoolean=function(value){
	if(value==true||value==false||value=="true"||value=="false"){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is email
 * @param value
 */
ValidatorUtils.isEmail=function(value,pattern){
	if(value==undefined||value==""){
	    return false;
	}
	if(pattern==undefined||pattern==""){
		var defaultPattern=/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
	    return defaultPattern.test(value);
	}
	if(typeof(pattern)=="string"){
	    pattern=new RegExp(pattern);
	}
	return pattern.test(value);
};

/**
 * Judge whether the value is URL
 * @param value
 */
ValidatorUtils.isUrl=function(value){
	var regx=/^[a-zA-z]+:\/\/[^\s]*$/;
    return regx.test(value);
};

/**
 * Judge whether the value is IP4
 * @param value
 */
ValidatorUtils.isIP4=function(value,strict){
	if(strict != null  && strict == ""){
		if (value == "0.0.0.0")
			return false;
		else if (value == "255.255.255.255")
			return false;
	};
	theName = "IPaddress";
	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = value.match(ipPattern);
	if (ipArray == null){
		return false;
	}else {
		for (i = 1; i < 5; i++){
			thisSegment = parseInt(ipArray[i]);
			if (thisSegment > 255) {
				return false;
			}
			if (i == 1 && parseInt(ipArray[1]) == 0 ) {
				return false ;
			}
		}
	}
	return true; 
};

/**
 * Judge whether the value is byte and between min and max
 * @param value
 * @param min
 * @param max
 */
ValidatorUtils.isByteInRange=function(value,min,max){
	if(!ValidatorUtils.isByte(value)||!ValidatorUtils.isByte(min)||!ValidatorUtils.isByte(max)){
		return false;
	}
	value=parseInt(value);
	min=parseInt(min);
	max=parseInt(max);
	if(value>=min&&value<=max){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is short and between min and max
 * @param value
 * @param min
 * @param max
 */
ValidatorUtils.isShortInRange=function(value,min,max){
	if(!ValidatorUtils.isShort(value)||!ValidatorUtils.isShort(min)||!ValidatorUtils.isShort(max)){
		return false;
	}
	value=parseInt(value);
	min=parseInt(min);
	max=parseInt(max);
	if(value>=min&&value<=max){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is integer and between min and max
 * @param value
 * @param min
 * @param max
 */
ValidatorUtils.isIntInRange=function(value,min,max){
	if(!ValidatorUtils.isInt(value)||!ValidatorUtils.isInt(min)||!ValidatorUtils.isInt(max)){
		return false;
	}
	value=parseInt(value);
	min=parseInt(min);
	max=parseInt(max);
	if(value>=min&&value<=max){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is long and between min and max
 * @param value
 * @param min
 * @param max
 */
ValidatorUtils.isLongInRange=function(value,min,max){
	if(!ValidatorUtils.isLong(value)||!ValidatorUtils.isLong(min)||!ValidatorUtils.isLong(max)){
		return false;
	}
	value=parseInt(value);
	min=parseInt(min);
	max=parseInt(max);
	if(value>=min&&value<=max){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is float and between min and max
 * @param value
 * @param min
 * @param max
 */
ValidatorUtils.isFloatInRange=function(value,min,max){
	if(!ValidatorUtils.isFloat(value)||!ValidatorUtils.isFloat(min)||!ValidatorUtils.isFloat(max)){
		return false;
	}
	value=parseInt(value);
	min=parseInt(min);
	max=parseInt(max);
	if(value>=min&&value<=max){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is double and between min and max
 * @param value
 * @param min
 * @param max
 */
ValidatorUtils.isDoubleInRange=function(value,min,max){
	if(!ValidatorUtils.isDouble(value)||!ValidatorUtils.isDouble(min)||!ValidatorUtils.isDouble(max)){
		return false;
	}
	value=parseInt(value);
	min=parseInt(min);
	max=parseInt(max);
	if(value>=min&&value<=max){
		return true;
	}
	return false;
};

/**
 * Judge whether the value length is smaller than or equal to max
 * @param value
 * @param max
 */
ValidatorUtils.maxLength=function(value,max,strict){
	if(!(value instanceof String)&&typeof(value)!="string"){
		value=new String(value);
	}
	var length=value.length;
	if(strict){
		length=StringUtils.charLength(value);
	}
	if(length<=max){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is less than or equal to max
 * @param value
 * @param max
 */
ValidatorUtils.maxValue=function(value,max){
	if(!ValidatorUtils.isDouble(value)||!ValidatorUtils.isDouble(max)){
		return false;
	}
	value=parseInt(value);
	max=parseInt(max);
	if(value<=max){
		return true;
	}
	return false;
};

/**
 * Judge whether the value length is larger than or equal to min
 * @param value
 * @param min
 */
ValidatorUtils.minLength=function(value,min,strict){
	if(!(value instanceof String)&&typeof(value)!="string"){
		value=new String(value);
	}
	var length=value.length;
	if(strict){
		length=StringUtils.charLength(value);
	}
	if(length>=min){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is larger than or equal to min
 * @param value
 * @param min
 */
ValidatorUtils.minValue=function(value,min){
	if(!ValidatorUtils.isDouble(value)||!ValidatorUtils.isDouble(min)){
		return false;
	}
	value=parseFloat(value);
	min=parseFloat(min);
	if(value>=min){
		return true;
	}
	return false;
};

/**
 * Judge whether the value is suitable to the expression
 * @param value
 * @param mask
 */
ValidatorUtils.isMask=function(value,mask){
	if(value==undefined||value==null){
		return false;
	}
	if(value==""&&mask!=undefined&&mask!=null){
		return true;
	}
	if((mask instanceof String)||typeof(mask)=="string"){
		var regx=new RegExp(mask,"gi");
		return regx.test(value);
	}
	if(mask instanceof RegExp){
		return mask.test(value);
	}
	return false;
};

/**
 * Judge whether the value is number
 * @param value
 */
ValidatorUtils.isNumber=function(value){
	return StringUtils.isNumeric(value);
};

/**
 * Judge whether the value is finite
 * @param value
 */
ValidatorUtils.isFinite=function(value){
	return isFinite(value);
};